package com.yyzw.activiti.modeler.controller;

import com.yyzw.activiti.modeler.model.DataGridRespVo;
import com.yyzw.activiti.modeler.model.ResponseVo;
import com.yyzw.activiti.modeler.model.SysBusiness;
import com.yyzw.activiti.modeler.model.SysBusinessVo;
import com.yyzw.activiti.modeler.service.SysBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Map;

@RestController
@RequestMapping("report")
public class BusinessReportController {


    @Autowired
    private SysBusinessService sysBusinessService;

    @RequestMapping("reportList")
    public ModelAndView reportManager(ModelAndView modelAndView){
        modelAndView.setViewName("report/reportList");
        return modelAndView;
    }

    @RequestMapping("deleteBusReport")
    public ResponseVo deleteBusReport(HttpServletRequest request)throws Exception{
        String busId = request.getParameter("busId");
        if(StringUtils.isBlank(busId)){
            return new ResponseVo(false,"请选择删除的数据");
        }
       return sysBusinessService.deleteBusReportBusId(Integer.parseInt(busId));
    }

    @RequestMapping("queryBusExtendByBusId")
    public DataGridRespVo queryBusExtendByBusId(HttpServletRequest request,String page,String rows)throws Exception{
        String busId = request.getParameter("busId");
        if(StringUtils.isBlank(busId)){
            return new DataGridRespVo();
        }
        return sysBusinessService.queryBusExtendByBusId(Integer.parseInt(busId),Integer.parseInt(page),Integer.parseInt(rows));
    }

    @RequestMapping("queryBusinessList")
    public DataGridRespVo queryBusinessList(HttpServletRequest request,String page,String rows) throws  Exception{

        return sysBusinessService.queryBusinessList(Integer.parseInt(page),Integer.parseInt(rows));
    }
    @RequestMapping("getReportById")
    public ResponseVo getReportById(HttpServletRequest request) throws Exception{
        String busId = request.getParameter("busId");
        if(StringUtils.isBlank(busId)){
            return new ResponseVo(false,"请选择修改的数据");
        }
        return sysBusinessService.getReportById(Integer.parseInt(busId));
    }

    @RequestMapping("prientBusReport")
    public ModelAndView prientBusReport(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String busId = request.getParameter("busId");
        if(StringUtils.isBlank(busId)){
            return null;
        }
        Map<String,Object> parameterMap = sysBusinessService.getPdfPrintInfo(busId);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition","attachement;filename="+ URLEncoder.encode("demo1","UTF-8") +".pdf");
        return new ModelAndView("pdfReport",parameterMap);
    }

    @RequestMapping("addBusReport")
    public ResponseVo addBusReport(@RequestBody SysBusinessVo sysBusinessVo,HttpServletRequest request) throws Exception{
      return sysBusinessService.addBusReport(sysBusinessVo);
    }

    @RequestMapping("updateBusReport")
    public ResponseVo updateBusReport(@RequestBody SysBusinessVo sysBusinessVo, HttpServletRequest request)throws  Exception{
        return sysBusinessService.updateBusReport(sysBusinessVo);
    }
}
